{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# An Introduction to Algorithms in Qiskit\n",
    "\n",
    "This is an introduction to algorithms in Qiskit and provides a high-level overview to help understand the various aspects of the functionality to get started. Other tutorials will provide more in-depth material, on given algorithms, and ways to use them etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How is the algorithm library structured?\n",
    "\n",
    "Qiskit provides a number of [Algorithms](https://qiskit.org/documentation/apidoc/qiskit.aqua.algorithms.html) and they are grouped by category according to the task they can perform. For instance `Minimum Eigensolvers` to find the smallest eigen value of an operator, for example ground state energy of a chemistry Hamiltonian or a solution to an optimization problem when expressed as an Ising Hamiltonian. There are `Classifiers` for machine learning  classification problems and `Amplitude Estimators` for value estimation that can be used say in financial applications. The full set of categories can be seen in the Algorithms documentation link above.\n",
    "\n",
    "Algorithms are configurable and often part of the configuration will be in the form of smaller building blocks, of which different instances of the building block type can be given. For instance with `VQE`, the Variational Quantum Eigensolver, it takes a trial wavefunction, in the form of a `QuantumCircuit` and a classical optimizer among other things. Such building blocks can be found as [Components](https://qiskit.org/documentation/apidoc/qiskit.aqua.components.html) and as circuits from the [Circuit Library](https://qiskit.org/documentation/apidoc/circuit_library.html).\n",
    "\n",
    "Let's take a look at an example to construct a VQE instance. Here `TwoLocal` is the variational form (trial wavefunction), a parameterized circuit which can be varied, and `SLSQP` a classical optimizer. These are created as separate instances and passed to VQE when it is constructed. Trying, for example, a different classical optimizer, or variational form is simply a case of creating an instance of the one you want and passing it into VQE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.aqua.algorithms import VQE\n",
    "from qiskit.aqua.components.optimizers import SLSQP\n",
    "from qiskit.circuit.library import TwoLocal\n",
    "\n",
    "num_qubits = 2\n",
    "ansatz = TwoLocal(num_qubits, 'ry', 'cz')\n",
    "opt = SLSQP(maxiter=1000)\n",
    "vqe = VQE(var_form=ansatz, optimizer=opt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's draw the ansatz so we can see it's a QuantumCircuit where θ\\[0\\] through θ\\[7\\] will be the parameters that are varied as VQE optimizer finds the minimum eigenvalue. We'll come back to the parameters later in a working example below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAB7CAYAAACIG9xhAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAV50lEQVR4nO3de1hUBf7H8ffMgAKCCqKSWAoilCiUl1K8oOWurD9/pmuWl9o0f1nolkputam1G6Vl2br7VFZb6rabukmmrmuWJeAFu2ipoBbeyUupqQmKIDC/PyZRAoajyJyZ6fN6nnmEc87DfPg4zHfOmcPBYrfb7YiIiBhgNTuAiIh4Dg0NERExTENDREQM09AQERHDNDRERMQwDQ0RETFMQ0NERAzT0BAREcM0NERExDANDRERMUxDQ0REDNPQEBERwzQ0RETEMA0NERExTENDREQM09AQERHDNDRERMQwDQ0RETFMQ0NERAzT0BAREcM0NERExDANDRERMUxDQ0REDNPQEBERwzQ0RETEMA0NERExzMfsAO7umzWQf9Sc+w5qBjG3mnPfZlDXrqOuXcesruuqZw2NGuQfhVMHzU7xy6CuXUddu463da3DUyIiYpiGhoiIGKahISIihmloiIiIYXoj/Cp5ZE5vdh7YiM3mi9VqIyw4ghG3TSExfqjZ0byKenYdde06ntS1hsZVNLLvNEb2nUppaQnLsl5mxoIRRIXfRHholNnRvIp6dh117Tqe0rUOT9UBm82H39xyP6VlJew5vMXsOF5LPbuOunYdd+9aQ6MOnC8pZkXWHABahkabnMZ7qWfXUdeu4+5da2hcRQs+eZZB0xoz4Al/5n04lZShbxLZIg6A6e+M4NMdK8q3fWr+IDZ985FZUatVVAL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Q0bgmdh5lz32ZQ166jrl3HrK7rqmftadQgyMQTocy8bzOoa9dR165j1vdbV/erPQ0RETFMb4SLiIhhGhoiImKYhoaIiBimoSEiIoZpaIiIiGEaGiIiYpiGhoiIGKahISIihmloiIiIYRoaIiJimIaGiIgYpqEhIiKGaWiIiIhhGhoiImKYhoaIiBimoSEiIoZpaIiIiGEaGiIiYtj/AztR363q98R/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 507.852x144.48 with 1 Axes>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ansatz.draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But more is needed before we can run the algorithm so let's get to that next."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to run an algorithm?\n",
    "\n",
    "In order to run an algorithm we need to have backend, a simulator or real device, on which the circuits that comprise the algorithm can be run. So for example we can use the `statevector_simulator` from the BasicAer provider."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import BasicAer\n",
    "\n",
    "backend = BasicAer.get_backend('statevector_simulator')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now a backend on its own does not have information on how you might want to run the circuits etc. For example how many shots, do you want a noise model, even options around transpiling the circuits. For this Qiskit Aqua has a [QuantumInstance](https://qiskit.org/documentation/stubs/qiskit.aqua.QuantumInstance.html) which is provided both the backend as well as various settings around the circuit processing and execution so for instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.aqua import QuantumInstance\n",
    "\n",
    "backend = BasicAer.get_backend('qasm_simulator')\n",
    "quantum_instance = QuantumInstance(backend=backend, shots=800, seed_simulator=99) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: if you provide the backend directly then internally a QuantumInstance will be created from it, with default settings, so at all times the algorithms are working through a QuantumInstance.\n",
    "\n",
    "So now we would be able to call the [run()](https://qiskit.org/documentation/stubs/qiskit.aqua.algorithms.VQE.html#qiskit.aqua.algorithms.VQE.run) method, which is common to all algorithms and returns a result specific for the algorithm. In this case since VQE is a MinimumEigensolver we could use the [compute_mininum_eigenvalue()](https://qiskit.org/documentation/stubs/qiskit.aqua.algorithms.VQE.html#qiskit.aqua.algorithms.VQE.compute_minimum_eigenvalue) method. The latter is the interface of choice for the application modules, such as Chemistry and Optimization, in order that they can work interchangeably with any algorithm within the specific category."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A complete working example\n",
    "\n",
    "Let's put what we have learned from above together and create a complete working example. VQE will find the minimum eigenvalue, i.e. minimum energy value of a Hamiltonian operator and hence we need such an operator for VQE to work with. Such an operator is given below. This was originally created by the Chemistry application module as the Hamiltonian for an H2 molecule at 0.735A interatomic distance. It's a sum of Pauli terms as below, but for now I am not going to say anything further about it since the goal is to run the algorithm, but further information on operators can be found in other tutorials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.aqua.operators import X, Z, I\n",
    "\n",
    "H2_op = (-1.052373245772859 * I ^ I) + \\\n",
    "        (0.39793742484318045 * I ^ Z) + \\\n",
    "        (-0.39793742484318045 * Z ^ I) + \\\n",
    "        (-0.01128010425623538 * Z ^ Z) + \\\n",
    "        (0.18093119978423156 * X ^ X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So let's build a VQE instance passing the operator, and a backend using a QuantumInstance, to the constructor. Now VQE does have setters so the operator and backend can also be passed later. Setting them later can be useful when creating an algorithm without this problem specific information and then later using it, say with the chemistry application module, which would create the operator for the specific chemistry problem being solved.\n",
    "\n",
    "Note: In order that you can run this notebook and see the exact same output the random number generator used throughout Aqua in aqua_globals, as well as the transpiler and simulator, via the QuantumInstance, are seeded. You do not have to do this but if want to be able to reproduce the exact same outcome each time then this is how it's done.\n",
    "\n",
    "So let's run VQE and print the result object it returns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{   'cost_function_evals': 72,\n",
      "    'eigenstate': array([-9.55448660e-05+2.12037105e-17j,  9.93766273e-01+2.25293943e-16j,\n",
      "       -1.11483565e-01+1.52657541e-16j, -1.77521351e-05+3.71607315e-17j]),\n",
      "    'eigenvalue': (-1.857275017559769+0j),\n",
      "    'optimal_parameters': {   Parameter(θ[0]): 4.296520551468743,\n",
      "                              Parameter(θ[1]): 4.426962086704216,\n",
      "                              Parameter(θ[2]): 0.5470753710293924,\n",
      "                              Parameter(θ[7]): 0.3602072316165878,\n",
      "                              Parameter(θ[6]): -4.717618235040379,\n",
      "                              Parameter(θ[5]): 1.5683261371389359,\n",
      "                              Parameter(θ[3]): 6.09294789784282,\n",
      "                              Parameter(θ[4]): -2.598325857134344},\n",
      "    'optimal_point': array([ 4.29652055,  4.42696209,  0.54707537,  6.0929479 , -2.59832586,\n",
      "        1.56832614, -4.71761824,  0.36020723]),\n",
      "    'optimal_value': -1.857275017559769,\n",
      "    'optimizer_evals': 72,\n",
      "    'optimizer_time': 1.310880184173584}\n"
     ]
    }
   ],
   "source": [
    "from qiskit.aqua import aqua_globals\n",
    "seed = 50\n",
    "aqua_globals.random_seed = seed\n",
    "qi = QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_transpiler=seed, seed_simulator=seed)\n",
    "\n",
    "ansatz = TwoLocal(rotation_blocks='ry', entanglement_blocks='cz')\n",
    "slsqp = SLSQP(maxiter=1000)\n",
    "vqe = VQE(operator=H2_op, var_form=ansatz, optimizer=slsqp, quantum_instance=qi)\n",
    "result = vqe.run()\n",
    "\n",
    "import pprint\n",
    "pp = pprint.PrettyPrinter(indent=4)\n",
    "pp.pprint(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the above result we can see it took the optimizer `72` evaluations of parameter values until it found the minimum eigenvalue of `-1.85727` which is the electronic ground state energy of the given H2 molecule. The optimal parameters of the ansatz can also be seen which are the values that were in the ansatz at the minimum value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using VQE as a MinimumEigensolver\n",
    "\n",
    "To close off let's use the MinimumEigensolver interface and also create a VQE instance without supplying either the operator or QuantumInstance. We later set the QuantumInstance and finally pass the operator on the `compute_minimum_eigenvalue` method (though we could have passed it in earlier via the setter instead, as long as by the time VQE runs it has an operator to work on)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{   'cost_function_evals': 72,\n",
      "    'eigenstate': array([-9.55448660e-05+2.12037105e-17j,  9.93766273e-01+2.25293943e-16j,\n",
      "       -1.11483565e-01+1.52657541e-16j, -1.77521351e-05+3.71607315e-17j]),\n",
      "    'eigenvalue': (-1.857275017559769+0j),\n",
      "    'optimal_parameters': {   Parameter(θ[4]): -2.598325857134344,\n",
      "                              Parameter(θ[5]): 1.5683261371389359,\n",
      "                              Parameter(θ[6]): -4.717618235040379,\n",
      "                              Parameter(θ[7]): 0.3602072316165878,\n",
      "                              Parameter(θ[1]): 4.426962086704216,\n",
      "                              Parameter(θ[0]): 4.296520551468743,\n",
      "                              Parameter(θ[2]): 0.5470753710293924,\n",
      "                              Parameter(θ[3]): 6.09294789784282},\n",
      "    'optimal_point': array([ 4.29652055,  4.42696209,  0.54707537,  6.0929479 , -2.59832586,\n",
      "        1.56832614, -4.71761824,  0.36020723]),\n",
      "    'optimal_value': -1.857275017559769,\n",
      "    'optimizer_evals': 72,\n",
      "    'optimizer_time': 2.8010470867156982}\n"
     ]
    }
   ],
   "source": [
    "aqua_globals.random_seed = seed\n",
    "qi = QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_transpiler=seed, seed_simulator=seed)\n",
    "\n",
    "ansatz = TwoLocal(rotation_blocks='ry', entanglement_blocks='cz')\n",
    "slsqp = SLSQP(maxiter=1000)\n",
    "vqe = VQE(var_form=ansatz, optimizer=slsqp)\n",
    "\n",
    "vqe.quantum_instance = qi\n",
    "result = vqe.compute_minimum_eigenvalue(operator=H2_op)\n",
    "\n",
    "pp.pprint(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As the identical seeding was used as the prior example the result can be seen to be the same."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This concludes this introduction to algorithms in Qiskit. Please check out the other algorithm tutorials in this series for both broader as well as more in depth coverage of the algorithms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>0.23.1</td></tr><tr><td>Terra</td><td>0.16.1</td></tr><tr><td>Aer</td><td>0.7.1</td></tr><tr><td>Ignis</td><td>0.5.1</td></tr><tr><td>Aqua</td><td>0.8.1</td></tr><tr><td>IBM Q Provider</td><td>0.11.1</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08) \n",
       "[GCC 7.5.0]</td></tr><tr><td>OS</td><td>Linux</td></tr><tr><td>CPUs</td><td>8</td></tr><tr><td>Memory (Gb)</td><td>31.40900421142578</td></tr><tr><td colspan='2'>Fri Jan 15 12:11:24 2021 UTC</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>&copy; Copyright IBM 2017, 2021.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import qiskit.tools.jupyter\n",
    "%qiskit_version_table\n",
    "%qiskit_copyright"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
